package com.jsm.tf.controller;

import com.jsm.tf.director.Director;
import com.jsm.tf.net.NettyClient;
import com.jsm.tf.net.message.request.RoomChatMessage;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;

import javax.annotation.Resource;
import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class RoomController implements Initializable {
    @FXML
    private Button logout;
    @FXML
    private Label nickname;
    @FXML
    private Label roomName;
    @FXML
    private Button start;
    @FXML
    private Button sendButton;
    @FXML
    private TextField contentTextField;
    @FXML
    private TextArea chatWindow;

    @Resource
    private Director director;
    @Resource
    private NettyClient nettyClient;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        nickname.setText("用户昵称:" + director.getUser().getNickname());
        roomName.setText("房间名:" + director.getRoom().getName());
        chatWindow.textProperty().addListener(new ChangeListener<Object>() {
            @Override
            public void changed(ObservableValue<?> observable, Object oldValue,
                                Object newValue) {
                chatWindow.setScrollTop(Double.MAX_VALUE);
                System.out.println("scrollTop");
            }
        });
    }


    public void sengChatMessage(MouseEvent mouseEvent) {
        String content = contentTextField.getText();
        RoomChatMessage roomChatMessage = new RoomChatMessage();
        roomChatMessage.setContent(content);
        nettyClient.sendMsg(roomChatMessage);
    }

    public void acceptRoomChatMessage(String sendUserNickname, String content) {
        contentTextField.clear();
        chatWindow.appendText("\r\n" + sendUserNickname + " : " + content);
    }


}
